#include "htmlforeach.h"
#include "parameter.h"
#include "body.h"
#include "template.h"
#include "errorlog.h"

HtmlForeach::HtmlForeach(Parameter* var, Parameter* index, Node* expr, Body* body, const YYLTYPE& loc)
        : Namespace( loc ), m_var( var ), m_index(index), m_expr(expr), m_body(body)
{
    appendChild( var );
    if ( index )
        appendChild( index );
    appendChild( expr );
    appendChild( body );
}

Node* HtmlForeach::getEntry( const QString& name )
{    
    if ( m_var->name() == name )
        return m_var;
    if ( m_index && m_index->name() == name )
        return m_index;
    return getLiftedEntry(name);
}

void HtmlForeach::setBodyTemplate( Template* t )
{
    m_bodyTemplate = t;
    appendChild(t);
}

void HtmlForeach::resolveAll()
{
    this->liftNames();
    this->Namespace::resolveAll();    

    if ( !m_expr->type().isList() )
        ErrorLog::error( m_expr->location(), "The expression must be a list" );
}
